<?php
/**
 * 反馈模块
 */

class Module_feedback{
	static private $white_uins = array(1 , 2 , 3 , 9 , 23);

	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	*反馈显示
	*/
	function index() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取用户数据
		loadLib('App_Overlord_Feedback');
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		//反馈信息
		$feedback_list = App_Overlord_Feedback::getList();

		$send_uin_list = array();
		foreach($feedback_list as $row) {
			$send_uin_list[] = $row['uin'];
			
			if(!empty($row['reply'])) {
				foreach($row['reply'] as $reply_row) {
					$send_uin_list[] = $reply_row['uin'];
				}
			}
		}

		$is_white_uin = $this->_is_white_uin($uin);
		
		if(!empty($send_uin_list)) {
			loadLib('User');
			$send_uin_info = User::getBaseInfo($send_uin_list);
			
			foreach($feedback_list as $k => $row){
				$send_uin = $row['uin'];
				$can_reply = $is_white_uin || $uin == $send_uin ? true : false;
				if (!empty($can_reply)){
					$list[$k] = $row;
					$list[$k]['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
					$list[$k]['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
					$list[$k]['can_reply'] = $is_white_uin || $uin == $send_uin ? true : false;
					if (!empty($row['reply'])){
						$list[$k]['count'] = count($row['reply']);
					}
				}
			}
		}
		if (!empty($list)){
			$feedback_count = count($list);
			$page = ($page-1) * $per_page;
			$list = array_slice($list,$page,$per_page);
			$smarty->assign('feedback_count' , $feedback_count);
			$smarty->assign('feedback_list' , $list);
		}
		$smarty->assign('per_page' , $per_page);

		return $smarty->fetch('app/Overlord/feedback.tpl.php');
	}


	/**
	*反馈写入
	*/
	function add() {
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		$uin = SessionInfo('uin');

		loadLib('App_Overlord_Feedback');
		$code = App_Overlord_Feedback::add($uin , $content);
		
		if($code === 0) {
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'feedback');		
			return View::success('反馈信息成功');
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}


	/**
	*反馈回复界面
	*/
	function send(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取反馈数据
		loadLib('App_Overlord_Feedback');
		//获取数据
		$feedback_send=App_Overlord_Feedback::getSend($id);
		
		if(empty($feedback_send['id'])){
			return View::error('该反馈信息已被删除' );
		}
		
			$send_uin_list = array();
			$send_uin_list[] = $feedback_send['uin'];
				
				if(!empty($feedback_send['reply'])) {
					foreach($feedback_send['reply'] as $reply_row) {
						$send_uin_list[] = $reply_row['uin'];
					}
				}
			
			if(!empty($send_uin_list)) {
				loadLib('User');
				$send_uin_info = User::getBaseInfo($send_uin_list);
				
				$send_uin = $feedback_send['uin'];
				
				$feedback_send['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$feedback_send['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				
				if(!empty($feedback_send['reply'])) {
					foreach($feedback_send['reply'] as $j => $reply_row) {
						$reply_uin = $reply_row['uin'];	
						$feedback_send['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
						$feedback_send['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
					}
				}
			}
			//设置每行10条数据
			$per_page = 6;
			if($page <= 0){
				$page = 1;
			}
			$count = count($feedback_send['reply']);
			$page = ($page-1) * $per_page;
			$send = array_slice($feedback_send['reply'],$page,$per_page);
			$smarty->assign('feedback_send' , $feedback_send);
			$smarty->assign('send' , $send);
			$smarty->assign('id' , $id);
			$smarty->assign('count' , $count);
			$smarty->assign('per_page' , $per_page);
			return $smarty->fetch('app/Overlord/send.tpl.php');
		
	}
	/**
	*反馈回复操作
	*/
	function upSend(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		if(empty($id)) {
			return View::error('参数错误' );
		}
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}

		$uin = SessionInfo('uin');
		
		loadLib('App_Overlord_Feedback');
		
		$org_feedback = App_Overlord_Feedback::getSend($id);
		if(empty($org_feedback)) {
			return View::error('该反馈信息不存在' , $code);
		}

		if( $org_feedback['uin'] != $uin && !$this->_is_white_uin($uin) ) {
			return View::error('没有权限回复' , $code);
		}

		
		$code = App_Overlord_Feedback::getUpSend($uin , $id , $content , $org_feedback);
		//print_r($org_feedback);
		if($code === 0) {
			if($uin != $org_feedback['uin']) {
				loadLib('Operate');
				Operate::getAdd($uin , $org_feedback['uin'] , 'feedback_reply');
			}		
			header('Location: ' . SITEURL . '/app.php?m=feedback&a=send&id=' . $id . '&sid=' . sessionInfo('skey') );
		} else {
			return View::error('该反馈信息已删除，操作失败' , $code);
		}
	}

	/**
	 * 是否是白名单用户
	 */
	private function _is_white_uin($uin) {
		return in_array($uin , self::$white_uins);
	}
	/**
	*删除反馈
	*/
	function delete(){
		
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if(empty($id)) {
			return View::error('参数错误' );
		}
		
		$uin = SessionInfo('uin');
		
		if( !$this->_is_white_uin($uin) ) {
			return View::error('您不是管理员，没有删除权限');
		}
		
		
		loadLib('App_Overlord_Feedback');
		
		$code = App_Overlord_Feedback::delete($id);
			
		if($code === 0) {		
			return View::success('已成功删除该信息');
		} else {
			return View::error('系统繁忙' , $code);
		}
		
	}
}


# end of script
